package com.shucoin.www.tycoin.ui.home.trade;

import com.shucoin.www.tycoin.AbsServicexUtils;
import com.shucoin.www.tycoin.Constant;
import com.shucoin.www.tycoin.adapter.DepthAdapter;
import com.shucoin.www.tycoin.bean.BaseResponse;
import com.shucoin.www.tycoin.bean.BidsBean;
import com.shucoin.www.tycoin.bean.DepthInfo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.http.RequestParams;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by happy on 2017/8/28.
 */

public class TradeP extends AbsServicexUtils{
    IViewTrade iViewTrade;
    public void setiViewTrade(IViewTrade iViewTrade) {
        this.iViewTrade = iViewTrade;
    }

    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
        depthInfoFormat(result,0);
    }

    private void bindDataToView(DepthInfo depthInfo){
        if (iViewTrade==null)
            return;
        DepthAdapter depthAdapter= iViewTrade.getAdapter(0);
        depthAdapter.getData().clear();
        depthAdapter.getData().addAll(depthInfo.getTick().getBids());
        depthAdapter.notifyDataSetChanged();
        DepthAdapter depthAdapterAsks= iViewTrade.getAdapter(1);
        depthAdapterAsks.getData().clear();
        depthAdapterAsks.getData().addAll(depthInfo.getTick().getAsks());
        depthAdapterAsks.notifyDataSetChanged();
    }


    public void depthInfoFormat(String result,int depth){
        int depthAsks=0;
        int depthBuy=0;
        switch (depth){
            case 0:
                depthBuy=4;
                depthAsks=0;
                break;
            case 1:
                depthBuy=9;
                depthAsks=5;
                break;
            case 2:
                depthBuy=14;
                depthAsks=9;
                break;
            case 3:
                depthBuy=19;
                depthAsks=14;
                break;
            case 4:

                break;
            case 5:
                break;
        }
        DepthInfo depthInfo=new DepthInfo();
        DepthInfo.TickBean tickBean=new DepthInfo.TickBean();
        List<BidsBean>  bidsBeanList=new ArrayList<>();
        List<BidsBean>  asksBeanList=new ArrayList<>();
        try {
            JSONObject jsonObject=new JSONObject(result);
            if (jsonObject.getString("status").equals("ok")){
                jsonObject=jsonObject.getJSONObject("tick");
                JSONArray bidsArray=jsonObject.getJSONArray("bids");
                JSONArray asksArray=jsonObject.getJSONArray("asks");//卖出
                for ( ; depthBuy >=(depth)*5; depthBuy--) { //买的再上面
                    BidsBean bidsBean=new BidsBean();
                    JSONArray subArray=bidsArray.getJSONArray(depthBuy);
                    bidsBean.setPrice(subArray.getString(0));
                    bidsBean.setCount(subArray.getString(1));
                    bidsBean.setType(1);
                    bidsBeanList.add(bidsBean);
                }
                for (  ; depthAsks < (depth+1)*5; depthAsks++) {
                    BidsBean bidsBean=new BidsBean();
                    JSONArray subArray=asksArray.getJSONArray(depthAsks);
                    bidsBean.setPrice(subArray.getString(0));
                    bidsBean.setCount(subArray.getString(1));
                    bidsBean.setType(-1);
                    asksBeanList.add(bidsBean);
                }
                tickBean.setBids(bidsBeanList);
                tickBean.setAsks(asksBeanList);
                depthInfo.setTick(tickBean);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        bindDataToView(depthInfo);
    }


    public void getDepthInfo(String sybmol, String type){
        RequestParams params=new RequestParams(Constant.DEPTH);
       // params.addBodyParameter("accessToken",audio.getAccessToken());
        params.addBodyParameter("symbol",sybmol);//	ethcny, etccny, bcccny, ethbtc, ltcbtc, etcbtc, bccbtc
        params.addBodyParameter("type",type);//	step0, step1, step2, step3, step4, step5（合并深度0-5）；step0时，不合并深度
        sendToServer(params);
    }


}
